import Tkinter
import tkMessageBox
import os
from Tkinter import *

top = Tkinter.Tk()
top.title("SQL tool")
global undoText

keys = ['BY', 'CREATE', 'DELETE', 'DROP', 'DATABASE', 'FROM', 'GROUP', 'INSERT', 'INTO', 'KEY', 'null', 'NOT', 'PRIMARY', 'SELECT', 'SHOW', 'TABLE', 'UPDATE', 'WHERE']

def buttonCallBack():
    command = entry.get()
    global undoText
    undoText = command
    entry.delete(0, len(command))
    print command

    if (command[0] == '<'):
        setText(os.popen('./sql ' + command).read())
    elif (command[0] == '-'):
        if (command == '-help'):
            help()
        else:
            setText(os.popen(command[1: len(command)]).read())
    else:
        tmpSql = open('tmp.sql','w')
        tmpSql.write(command)
        tmpSql.close()
        setText(os.popen('./sql tmp.sql').read())
        os.system('rm tmp.sql')

def dealTab():
    pre = entry.get().split(' ')[-1]
    if (pre != ''):
        for key in keys:
            if (key[0 : len(pre)] is pre):
                entry.insert(Tkinter.INSERT, key[len(pre) : len(key)])
                break

def checkKey(event):
    if (len(event.char) == 1):
        if (ord(event.char) is 13):
            buttonCallBack()
        if (ord(event.char) is 9):
            dealTab()

def undo():
    global undoText
    entry.delete(0, len(entry.get()))
    entry.insert(Tkinter.INSERT, undoText)

def setText(str):
    text.delete(0.0, Tkinter.END)
    text.insert(Tkinter.INSERT, str)

def help():
    setText('Welcome to SQL tool!\n"-help" for help\n"-???" for system command\n"<???" to execute SQL in ???.sql\n"???" to execute SQL "???"\nTAB for automatic completion')

text = Tkinter.Text(top)
text.pack(side = Tkinter.BOTTOM)
help()

label = Tkinter.Label(top, text = "command : ")
label.pack(side = Tkinter.LEFT)

entry = Tkinter.Entry(top, bd = 1, width = 50)
entry.pack(side = Tkinter.LEFT)
entry.bind('<Key>', checkKey)
entry.focus_set()

button = Tkinter.Button(top, text = "confirm", command = buttonCallBack)
button.pack(side = Tkinter.LEFT)

def insertCommand(str):
    entry.insert(Tkinter.INSERT, ' ' + str)

def donothing():
   filewin = Toplevel(top)
   button = Button(filewin, text="Do nothing button")
   button.pack()

menubar = Menu(top)
startMenu = Menu(menubar, tearoff = 0)
startMenu.add_command(label = "Help", command = help)
startMenu.add_command(label = "Undo", command = undo)
startMenu.add_separator()
startMenu.add_command(label = "Exit", command = top.quit)
menubar.add_cascade(label = "Start", menu = startMenu)

operateMenu = Menu(menubar, tearoff = 0)
operateMenu.add_command(label = "CREATE", command = lambda: insertCommand('CREATE'))
operateMenu.add_command(label = "DELETE", command = lambda: insertCommand('DELETE'))
operateMenu.add_command(label = "DROP", command = lambda: insertCommand('DROP'))
operateMenu.add_command(label = "INSERT", command = lambda: insertCommand('INSERT'))
operateMenu.add_command(label = "SHOW", command = lambda: insertCommand('SHOW'))
operateMenu.add_command(label = "SELECT", command = lambda: insertCommand('SELECT'))
operateMenu.add_command(label = "UPDATE", command = lambda: insertCommand('UPDATE'))
menubar.add_cascade(label = "Operate", menu = operateMenu)

prepMenu = Menu(menubar, tearoff = 0)
prepMenu.add_command(label = "BY", command = lambda: insertCommand('CREATE'))
prepMenu.add_command(label = "FROM", command = lambda: insertCommand('FROM'))
prepMenu.add_command(label = "INTO", command = lambda: insertCommand('INTO'))
prepMenu.add_command(label = "WHERE", command = lambda: insertCommand('WHERE'))
menubar.add_cascade(label = "Prep", menu = prepMenu)

nounMenu = Menu(menubar, tearoff = 0)
nounMenu.add_command(label = "DATABASE", command = lambda: insertCommand('DATABASE'))
nounMenu.add_command(label = "GROUP", command = lambda: insertCommand('GROUP'))
nounMenu.add_command(label = "KEY", command = lambda: insertCommand('KEY'))
nounMenu.add_command(label = "TABLE", command = lambda: insertCommand('TABLE'))
menubar.add_cascade(label = "Noun", menu = nounMenu)

top.config(menu = menubar)
top.mainloop()
